<?php
namespace app\base\controller;

use think\Db;
use think\Request;


/**
 * 个人资料管理
 * uncheck
 */
class User extends Common
{

    public function _initialize()
    {
        parent::_initialize();
        $this->assign('vo',session('manager'));
    }

    /**
     * 首页
     */
    public function index()
    {

        return $this->fetch();
    }

    /**
     * 保存编辑资料
     */
    public function save_profile(){
        $post = input('post.');
        if(isset($post['login_name']))$this->error('登录名称不可修改');
        $data['truename'] = $post['truename'];
        $data['mobile'] = $post['mobile'];
        $data['email'] = $post['email'];
        $data['id'] = session('manager.id');

        $manager = new Manager;
        $flag = $manager->isUpdate(true)->save($data);
        if($flag){
            $arr = array_merge(session('manager'),$post);
            session('manager',$arr);

            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }

    }

    /**
     *修改密码
     */
    public function repwd(){
        if($this->request->isPost()){
            $post = input('post.');
            $manager = new Manager();

            $old_info = $manager->field('login_pwd,encrypt')->find(session('manager.id'));

            $oldpwd = $manager->make_pwd_md5($post['oldpwd'] , $old_info->encrypt);

            if($oldpwd !== $old_info->login_pwd) $this->error('旧密码不正确');
            if($post['newpwd'] !== $post['renewpwd']) $this->error('新密码，两次输入的不一致');

            
            $update['login_pwd'] = $post['newpwd'];
            $update['id'] = session('manager.id');


            //不走模型避免触发修改器
            //$flag = Db::table($manager->getTable())->data($update)->update();
            $flag = $manager->isUpdate(true)->save($update);
            

            $flag ? $this->success('修改成功') : $this->error('修改失败');
        }

        if($this->request->isGet()){
            return $this->fetch();
        }

    }
    /**
     * 保存修改密码
     */
    public function change_pwd_action(){

    }

    /**
     * 上传图像
     */
    public function avatar(){
        return $this->fetch();
    }

    /**
     * 保存会员头像
     */
    function avatar_action(){
        $post = input('post.');

        $flag = Db::table('jy_manager')
            ->where(['id'=> session('manager.id')])
            ->update(['avatar' => $post['img']]);

        if($flag){
            session('manager.avatar' ,$post['img']);
            $this->success('上传成功');
        }else{
            @unlink(__ROOT__.$post['img']);

        }

    }

    /**
     * 查看操作日志
     */
    public function action_log(){

    }



}
